#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "driver/uart.h"
#include "sdkconfig.h"
#include "voice.h"

// 定义全局变量
const uint8_t PrevFile[] = {0xAA, 0x05, 0x00, 0xAF};
const uint8_t NextFile[] = {0xAA, 0x06, 0x00, 0xB0};
const uint8_t Path[] = {0xAA, 0x08, 0x10, 0x02, 0x2F, 0xB9, 0xE3, 0xB8, 0xE6, 0x2A, 0x2F, 0xD0, 0xA1, 0xC3, 0xD7, 0x2A, 0x4D, 0x50, 0x33, 0x8B};


const char* TAG = "UART_MP3";

// 初始化UART
void voice_init_uart(void)
{
    const uart_config_t uart_config = {
        .baud_rate = 9600,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
        .source_clk = UART_SCLK_APB,
    };

    // 安装UART驱动
    ESP_ERROR_CHECK(uart_driver_install(UART_NUM, UART_BUFFER_SIZE, 0, 0, NULL, 0));
    // 配置UART参数
    ESP_ERROR_CHECK(uart_param_config(UART_NUM, &uart_config));
    // 设置UART引脚
    ESP_ERROR_CHECK(uart_set_pin(UART_NUM, UART_TXD_PIN, UART_RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
}

// 发送数据
void send_data(const uint8_t* data, size_t len)
{
    int bytes_written = uart_write_bytes(UART_NUM, data, len);
    if (bytes_written < 0)
    {
        ESP_LOGE(TAG, "Failed to send data");
    }
    else
    {
        ESP_LOGI(TAG, "Data sent successfully: %d bytes", bytes_written);
    }
}

size_t get_PrevFile_size(void) {
    return sizeof(PrevFile);
}

size_t get_NextFile_size(void) {
    return sizeof(NextFile);
}

size_t get_Path_size(void) {
    return sizeof(Path);
}